const CORE_CASES: any = new Proxy({}, {
    set(target: any, key: string, value: Tfunction<void>) {
        if (!isBrowser && !target.hasOwnProperty(key)) {// NodeJs环境时，添加属性时即执行。
            console.log(`================================================================================= ${key} =================================================================================`);
            value();
        }
        target[key] = value;
        return true;
    }
});
const DOM_CASES: any = {};
const PLUGINS_CASES: any = {};
if (isBrowser && true === (globalThis as any)['isTest']) {// 浏览器环境，在页面加载完成时执行。
    window.onload = () => {
        (globalThis as any)['isTest'] = true;// 启用测试环境
        if (window.location.pathname === '/test.html') {// 主测试页面，不做任何测试。
        } else if (window.location.pathname === '/core/test.html') {// core主测试页面，只测试基础方法。
            for (const key in CORE_CASES) {
                if (Object.prototype.hasOwnProperty.call(CORE_CASES, key)) {
                    console.log(` ================== CORE - ${key} ================== `);
                    CORE_CASES[key]();
                }
            }
        } else if (window.location.pathname === '/core/DOM/test.html') {// DOM测试主页面，创建对应测试页面的iframe进行测试。
            for (const key in DOM_CASES) {
                if (Object.prototype.hasOwnProperty.call(DOM_CASES, key)) {
                    const ele_div = document.createElement('div');
                    ele_div.style.margin = '10px';
                    ele_div.style.border = '1px solid #339999';
                    ele_div.id = key;

                    const ele_title = document.createElement('div');
                    ele_title.innerHTML = key;
                    ele_title.style.padding = '3px 6px';
                    ele_title.style.backgroundColor = '#339999';
                    ele_title.style.color = '#ffffff';
                    ele_div.append(ele_title);

                    const ele_iframe = document.createElement('iframe');
                    ele_iframe.src = `${DOM_CASES[key].src}.test.html`;
                    ele_iframe.style.width = '100%';
                    ele_iframe.style.height = '100px';
                    ele_iframe.style.border = 'none';
                    ele_div.append(ele_iframe);

                    document.body.append(ele_div);
                }
            }
        } else if (window.location.pathname === '/plugins/test.html') {// plugins测试主页面，创建对应测试内容进行测试。
            const noArgs = Symbol();
            function case_wrapper(funcName: string, ele_cases: KHTMLElement, value: any, fn: () => KHTMLElement) {
                const case_wrapper = ele_cases.createElement('div').addClass('case-wrapper');

                if (funcName === 'text') {
                    const title_string = value === noArgs ? '' : K.text({ text: value, type: 'string' }).ele()!.creator!.options.text;
                    case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', '(string) => ' + title_string);
                    const title_default = value === noArgs ? '' : K.text({ text: value, type: 'default' }).ele()!.creator!.options.text;
                    case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', '(default) => ' + title_default);
                }

                const title_input = value === noArgs ? '' : K.text({ text: value, type: 'title' }).ele()!.creator!.options.text;
                case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', `input${funcName === 'text' ? '(title)' : ''} => ` + title_input);

                let plugin;
                try {
                    plugin = fn();
                } catch (error) {
                    console.log(error);
                }

                if (plugin) {
                    case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', `after_handle${funcName === 'text' ? '(code-format)' : ''} =>`);
                    // @ts-ignore Property 'options_after_handle' is private and only accessible within class 'Coverloading<T>'.ts(2341)
                    const title_after_handle = K.text({ text: plugin.ele()!.creator!.options_after_handle, type: 'code-format' });
                    case_wrapper.createElement('div').addClass('case-title').addClass('exe').insertElement(title_after_handle);

                    if (funcName === 'text') {
                        case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', `after_handle${funcName === 'text' ? '(code)' : ''} =>`);
                        // @ts-ignore Property 'options_after_handle' is private and only accessible within class 'Coverloading<T>'.ts(2341)
                        const title_after_handle2 = K.text({ text: plugin.ele()!.creator!.options_after_handle, type: 'code' });
                        case_wrapper.createElement('div').addClass('case-title').addClass('exe').insertElement(title_after_handle2);
                    }

                    case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', `exe${funcName === 'text' ? '(code)' : ''} =>`);
                    const title_exe = K.text({ text: plugin.ele()!.creator!.options, type: 'code' });
                    case_wrapper.createElement('div').addClass('case-title').addClass('exe').insertElement(title_exe);

                    if (funcName === 'text') {
                        case_wrapper.createElement('div').addClass('case-title').setAttr('textContent', '(text) => ');
                    }

                    case_wrapper.createElement('div').addClass('case').insertElement(plugin);
                } else {
                    case_wrapper.createElement('div').addClass('case-title').addClass('error').setAttr('textContent', 'error');
                }
            }
            function group_wrapper(container: any, funcName: string, title: string, values: 'noArgs' | Array<any>, fn: (value?: any) => KHTMLElement, ext?: boolean) {
                const group_wrapper = K(container).createElement('div').addClass('group-wrapper').addClass(ext ? 'exts' : '')
                    .insertElement('div', {
                        attributes: { textContent: `K.${funcName}(${title})`, className: 'group-title' },
                    });
                const ele_cases = group_wrapper.createElement('div').addClass('cases');
                if (values === 'noArgs') {
                    case_wrapper(funcName, ele_cases, noArgs, () => { return fn(); });
                } else {
                    values.forEach(value => {
                        case_wrapper(funcName, ele_cases, value, () => { return fn(value); });
                    });
                }
            }
            function test(container: any, funcName: string, fn: Function, keys: string[], values: Array<any>, extendsValues?: { 'title': string, 'values': Array<any> }[]) {
                group_wrapper(container, funcName, '', 'noArgs', () => { return fn(); });
                group_wrapper(container, funcName, 'DEFAULT', [DEFAULT], (options) => { return fn(options); });
                group_wrapper(container, funcName, '{}', [{}], (options) => { return fn(options); });
                group_wrapper(container, funcName, '[]', [[]], (options) => { return fn(options); });

                if (values.length !== 0) {
                    group_wrapper(container, funcName,
                        '?',
                        values,
                        (value) => { return fn(value); }
                    );

                    group_wrapper(container, funcName,
                        '{x: ?}',
                        values.map(value => ({ x: value })),
                        (value) => { return fn(value); }
                    );

                    group_wrapper(container, funcName,
                        '[?]',
                        values.map(value => ([value])),
                        (value) => { return fn(value); }
                    );

                    keys.forEach(key => {
                        group_wrapper(container, funcName,
                            `{ ${key}: ? }`,
                            values.map(value => ({ [key]: value })),
                            (options) => { return fn(options); }
                        );
                        group_wrapper(container, funcName,
                            `{ ${key}: {x: ?} }`,
                            values.map(value => ({ [key]: { x: value } })),
                            (options) => { return fn(options); }
                        );
                        group_wrapper(container, funcName,
                            `{ ${key}: [?] }`,
                            values.map(value => ({ [key]: [value] })),
                            (options) => { return fn(options); }
                        );
                    });
                }

                if (extendsValues) {
                    extendsValues.forEach(ev => {
                        group_wrapper(container, funcName,
                            ev.title,
                            ev.values,
                            (value) => { return fn(value); },
                            true
                        );
                    });
                }
            }

            for (const key in PLUGINS_CASES) {
                if (Object.prototype.hasOwnProperty.call(PLUGINS_CASES, key)) {
                    if (!PLUGINS_CASES[key].test) { continue; }
                    const color = U.D.randomColor();

                    const ele_div = document.createElement('div');
                    ele_div.className = 'plugin-test';
                    ele_div.style.border = `3px solid ${color}`;
                    ele_div.id = key;
                    ele_div.addEventListener('click', (e) => {
                        const classList = (e.target as HTMLElement).classList;
                        if (classList.contains('plugin-test-title') || classList.contains('group-title')) {
                            (e.target as HTMLElement).parentElement!.style.display = 'none';
                        }
                    })

                    const ele_title = document.createElement('div');
                    ele_title.className = 'plugin-test-title';
                    ele_title.innerHTML = key;
                    ele_title.style.backgroundColor = color;
                    ele_title.style.color = U.D.invertColor(color);
                    ele_div.append(ele_title);

                    const ele_container = document.createElement('div');
                    ele_container.className = 'groups';
                    ele_div.append(ele_container);

                    document.body.append(ele_div);
                    PLUGINS_CASES[key].before();
                    test(ele_container, key, (K as any)[key], PLUGINS_CASES[key].keys, PLUGINS_CASES[key].values, PLUGINS_CASES[key].extendsValues(PLUGINS_CASES[key].values));
                    PLUGINS_CASES[key].after(ele_container, group_wrapper);
                }
            }
        } else {// DOM测试子页面，只执行对应的测试。
            const match = window.location.pathname.match(/[.]*\/([a-zA-Z0-9]+).test.html/);
            if (match) {
                const key = match[1];
                console.log(` ================== DOM - ${key} ================== `);
                (globalThis as any).funcName = key;
                DOM_CASES[key].fn();
            }
        }
        (globalThis as any)['isTest'] = false;// 停用测试环境
    };
}
